<?php 

class Order_ViewController extends Zend_Controller_Action{

	const PERMISSION_DENIED = 1;
	const RESULT_NOT_FOUND = 2;
		
	protected $_searchQuery;
	
	public function init(){
		$this->_searchQuery = new Order_Model_View();
	}
	
	public function indexAction(){
		
		$itemCountPerPage = 10;
		
		$form = new Order_Form_SearchOrder(array(
			'action'	=>	'/order/view',
			'method'	=>	'post'
		));
		
		$request = $this->getRequest();
		if($request->isPost()){
			$this->_searchQuery->create($request->getPost());
			//$itemCountPerPage = $request->getPost('itemCountPerPage');
		}
		//var_dump($this->_searchQuery->getQuery());
        $this->view->form = $form;		
		/**
         * Paginator
         */
        if(is_object($this->_searchQuery->getQuery())){
	        $adapter = new Custom_Paginator_Adapter_Doctrine($this->_searchQuery->getQuery());
	        
	        $paginator = new Zend_Paginator( $adapter );
	        $paginator->setItemCountPerPage( $itemCountPerPage )
	                  ->setCurrentPageNumber( $this->_getParam( 'page', 1 ) )
	                  ->setPageRange( 8 );

			$this->view->paginator = $paginator;
        }
          
        $this->view->form = $form;
	}
	
	public function detailsAction(){
		
		$request = $this->getRequest();
		
		if($request->orderId){
			$orderId = $request->orderId;
		}else{
			throw new exception(self::RESULT_NOT_FOUND);
		}
		
		try{
			$this->view->order = Doctrine_Model_Orders::getOrderDetails($orderId);
			$this->view->orderItems = Doctrine_Model_OrderItems::getOrderItems($orderId);
		}catch(Exception $e){
			switch($e->getMessage()){
				case 1:
					$this->view->message = "Permission Denied";
					break;
				case 2:
					$this->view->message = "Order Does Not Exist";
					break;
			}
			
		}	
	}
}